package com.eeepay.eeepay_v2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2._recadapter.CJTInComeInfoListAdapter;
import com.eeepay.eeepay_v2._recadapter.CommonLinerRecyclerView;
import com.eeepay.eeepay_v2.api.GetSuperPushShareRecordAPI;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import cn.eeepay.api.grpc.nano.ManagementCenterProto;

/**
 * 描述：超级推收益明细页面
 * 作者：zhuangzeqin
 * 时间: 2017/8/21-14:49
 * 邮箱：zzq@eeepay.cn
 */
public class CjtInComeRecordFragment extends ABBaseFragment{
    private static final int CJTREQUSETCODE= 478;//请求的任务id
    private CJTInComeInfoListAdapter adapter;
    private CommonLinerRecyclerView recyclerView;
    private RefreshLayout refreshLayout;
    private int currPage = 1;//当前页
    private int mTotal = 0;//总的记录数
    private GetSuperPushShareRecordAPI mGetSuperPushShareRecordAPI;//获取超级推分润查询记录GRPC
    /**
     * 获得创建实例
     * @return
     */
    public static CjtInComeRecordFragment  getInstance()
    {
        CjtInComeRecordFragment cjtInComeRecordFragment = new CjtInComeRecordFragment();
        return cjtInComeRecordFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mGetSuperPushShareRecordAPI = new GetSuperPushShareRecordAPI();
        mGetSuperPushShareRecordAPI.setResultCallBack(resultCallBack);
        adapter = new CJTInComeInfoListAdapter(context);
    }

    /**
     * 数据的接口的监听回调
     */
    private final GetSuperPushShareRecordAPI.ResultCallBack resultCallBack = new GetSuperPushShareRecordAPI.ResultCallBack() {
        @Override
        public void oninit() {

        }

        @Override
        public void onFail(String msg) {
            showToast(msg);
        }

        @Override
        public void onSucess(int currPage, int total, List<ManagementCenterProto.SuperPushShareRecord> superPushShareRecords) {
            mTotal = total;//获取总的记录数
            if (currPage==1) {
                adapter.setList(superPushShareRecords);
            }
            else {
                adapter.addItemFromList(superPushShareRecords);
            }
        }
        @Override
        public void onUnInit() {

        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_cjtincomelistrecord, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (CommonLinerRecyclerView)view.findViewById(R.id.recyclerview);
        recyclerView.setAdapter(adapter);
        refreshLayout =(RefreshLayout)view.findViewById(R.id.refreshLayout);//刷新加载框框
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(final RefreshLayout refreshlayout) {//加载更多
                if (mTotal > currPage * GetSuperPushShareRecordAPI.PAGESIZE) {//如果还小于总的大小；接着请求下10条记录
                    currPage++;
                    sendHttpRequest(CJTREQUSETCODE);
                    refreshLayout.finishLoadmore(100);
                }
                else
                {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showToast("已经是最后一页了");
                            refreshlayout.finishLoadmore(500);
                        }
                    });
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {//刷新
                currPage = 1;
                sendHttpRequest(CJTREQUSETCODE);
                refreshLayout.finishRefresh(100);

            }
        });
        refreshLayout.autoRefresh();//自动刷新
    }

    /**
     * 开始网络请求
     * @param taskId
     */
    @Override
    protected void sendHttpRequest(int taskId) {
        if (taskId==CJTREQUSETCODE)
            mGetSuperPushShareRecordAPI.getSuperPushShareRecordInfo(currPage);
    }
}
